﻿/***********************************************************************
 * <copyright file="UserController.cs" company="B/S JSC">
 * -->    Copyright (C) statement. All right reserved
 * </copyright>
 * 
 * Created:   Hoang Bich Son 
 * Email:    sonhb@newman.vn
 * Website:
 * Create Date: Saturday, March 29, 2014
 * Usage: 
 * 
 * RevisionHistory: 
 * Date         Author               Description 
 * 
 * ************************************************************************/

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Web.Mvc;
using DXWebApplication.Areas.Administrators.Controllers;
using DxMvcApplication.Areas.Administrator.Models;
using DxMvcApplication.Repositories.Implementations.Security;
using Newtonsoft.Json;

namespace DxMvcApplication.Areas.Administrator.Controllers.Security
{
    public class UserController : AdminBaseController, IAdminBaseController<UserModel>
    {
        private readonly UserRepository _userRepository;
        private readonly RoleRepository _roleRepository;

        public UserController()
        {
            _userRepository = new UserRepository();
            _roleRepository = new RoleRepository();
        }

        [HttpGet]
        public PartialViewResult Insert()
        {
            var result = new UserModel();
            ViewBag.Roles = _roleRepository.GetList();
            return PartialView("UserDetail", result);
        }
        /// <summary>
        /// Inserts the specified model.
        /// </summary>
        /// <param name="model">The model.</param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult Insert(UserModel model)
        {
            var errorCode = string.Empty;
            if (ModelState.IsValid)
            {
                errorCode = _userRepository.Insert(model.FromModel());
            }

            return Json(new { message = errorCode });
        }

        /// <summary>
        /// Updates this instance.
        /// </summary>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        [HttpGet]
        public PartialViewResult Update(string idJson)
        {
            var idList = JsonConvert.DeserializeObject<List<int>>(idJson);
            var model = new UserModel();
            if (idList.Count == 1)
            {
                int id;
                int.TryParse(idList[0].ToString(CultureInfo.InvariantCulture), out id);
                if (id > 0)
                {
                    ViewBag.Roles = _roleRepository.GetList();
                    model = _userRepository.Get(id).ToModel();
                    model.Password = model.ConfirmPassword = "10aaf454c194b23c9163c107b12c958b";
                }
            }
            return PartialView("UserDetail", model);
        }

        /// <summary>
        /// Updates the specified model.
        /// </summary>
        /// <param name="model">The model.</param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult Update(UserModel model)
        {
            var msg = string.Empty;
            if (ModelState.IsValid)
            {
                msg = _userRepository.Update(model.FromModel());
            }
            return Json(new { message = msg });
        }

        /// <summary>
        /// Deletes the specified ids.
        /// </summary>
        /// <param name="idJson">The identifier json.</param>
        /// <returns></returns>
        public JsonResult Delete(string idJson)
        {
            string msg;
            try
            {
                var idList = JsonConvert.DeserializeObject<IList<int>>(idJson);
                msg = _userRepository.Delete(idList);
            }
            catch (Exception)
            {
                return Json(new { message = NmResources.MessageResource.Exception }, JsonRequestBehavior.AllowGet);
            }
            return Json(new { message = msg }, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// Gets the paged.
        /// </summary>
        /// <param name="currentPage">The current page.</param>
        /// <returns></returns>
        [HttpGet]
        public PartialViewResult GetPaged(int currentPage = 1)
        {
            var totalPage = 0;
            var result = _userRepository.GetPaged(ref totalPage, currentPage);
            ViewBag.TotalPage = totalPage;
            ViewBag.CurrentPage = currentPage;
            return PartialView("User", result.ToModel());
        }

        /// <summary>
        /// Gets the paged.
        /// </summary>
        /// <param name="keyword">The keyword.</param>
        /// <param name="status">The status.</param>
        /// <param name="sort">The sort.</param>
        /// <param name="order">The order.</param>
        /// <param name="totalPage">The total page.</param>
        /// <param name="currentPage">The current page.</param>
        /// <returns></returns>
        [HttpPost]
        public PartialViewResult GetPaged(string keyword = null, bool? status = null, string sort = "", string order = "", int totalPage = 0, int currentPage = 1)
        {            
            var result = _userRepository.GetPaged(ref totalPage, currentPage);
            ViewBag.TotalPage = totalPage;
            ViewBag.CurrentPage = currentPage;
            return PartialView("UserList", result.ToModel());
        }

    }
}
